﻿@namespace BulmaRazor.Components
@using System.Text.RegularExpressions
@inherits InputBase<string>
@inject IJSRuntime JsRuntime
<input id="@Id" @attributes="Attributes" @bind-value="ShowValue" @bind-value:event="@BindEvent"
       class="@classes"/>

@code {


    [Parameter]
    public ColorPickerOptions Options { get; set; }

    private string ShowValue
    {
        get { return Value; }
        set
        {
            Value = value;
            ValueChanged.InvokeAsync(Value).GetAwaiter().GetResult();
        }
    }

    // [Parameter]
    // public EventCallback<TagsInput> AfterAdd { get; set; }
    //
    // [Parameter]
    // public EventCallback<TagsInput> AfterRemove { get; set; }


    protected override void OnInitialized()
    {
        Options ??= new ColorPickerOptions();
    // JSCallbackManager.AddEventHandler(Id, "after.add", new Func<Dictionary<string, object>, Task>((dict) =>
    // {
    //     ShowValue = dict["value"].ToString();
    //     return AfterAdd.InvokeAsync(this);
    // }));
    // JSCallbackManager.AddEventHandler(Id, "after.remove", new Func<Dictionary<string, object>, Task>((dict) =>
    // {
    //     ShowValue = dict["value"].ToString();
    //     return AfterRemove.InvokeAsync(this);
    // }));
    // JSCallbackManager.AddEventHandler(Id, "after.flush", new Func<Dictionary<string, object>, Task>((dict) =>
    // {
    //     ShowValue = "";
    //     return AfterRemove.InvokeAsync(this);
    // }));
        base.OnInitialized();
    }

    protected override ValueTask DisposeAsync(bool disposing)
    {
        JSCallbackManager.DisposeObject(Id);
        return base.DisposeAsync(disposing);
    }

    private IJSObjectReference jsObj;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            jsObj = await JsRuntime.InvokeAsync<IJSObjectReference>("BulmaRazor.NewJsColor", "#" + Id, Options.ToParams());
        }
    }

    public ValueTask<double> Channel(string name)
    {
        return jsObj.InvokeAsync<double>("channel", name);
    }

    public ValueTask<bool> Channel(string name, double value)
    {
        return jsObj.InvokeAsync<bool>("channel", name, value);
    }

    public ValueTask<bool> FromHSVA(double? h, double? s, double? v, double? a)
    {
        return jsObj.InvokeAsync<bool>("fromHSVA", h, s, v, a);
    }

    public ValueTask<bool> FromRGBA(double? r, double? g, double? b, double? a)
    {
        return jsObj.InvokeAsync<bool>("fromRGBA", r, g, b, a);
    }

    public ValueTask<bool> FromString(string str)
    {
        return jsObj.InvokeAsync<bool>("fromString", str);
    }

    public ValueTask<string> GetFormat()
    {
        return jsObj.InvokeAsync<string>("getFormat");
    }

    public ValueTask<bool> IsLight()
    {
        return jsObj.InvokeAsync<bool>("isLight");
    }

    public ValueTask Hide()
    {
        return jsObj.InvokeVoidAsync("hide");
    }

    public ValueTask Show()
    {
        return jsObj.InvokeVoidAsync("show");
    }

    public ValueTask<string> ToBackground()
    {
        return jsObj.InvokeAsync<string>("toBackground");
    }

    public ValueTask<double> ToGrayscale()
    {
        return jsObj.InvokeAsync<double>("toGrayscale");
    }

    public ValueTask<string> ToHEXAString()
    {
        return jsObj.InvokeAsync<string>("toHEXAString");
    }

    public ValueTask<string> ToHEXString()
    {
        return jsObj.InvokeAsync<string>("toHEXString");
    }

    public ValueTask<string> ToRGBAString()
    {
        return jsObj.InvokeAsync<string>("toRGBAString");
    }

    public ValueTask<string> ToRGBString()
    {
        return jsObj.InvokeAsync<string>("toRGBString");
    }

    public ValueTask<string> ToString(string format)
    {
        return jsObj.InvokeAsync<string>("toString", format);
    }

}